/*
by txdnet.cn tonydon
 */

package txdnet.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Logger.error(this.getClass().getName(),"Error information!");
 * @author txdnet
 */

public final class Logger {
    
private Logger(){}

public static void debug(Class clazz, String debugStr) {
   Log log = LogFactory.getLog(clazz);
   if (log.isDebugEnabled()) {
     log.debug(debugStr);
   }
}

  public static void info(Class clazz, String infoStr) {
   Log log = LogFactory.getLog(clazz);
   if (log.isInfoEnabled()) {
     log.info(infoStr);
   }
 }


  public static void warn(Class clazz, String warnStr) {
   Log log = LogFactory.getLog(clazz);
   if (log.isWarnEnabled()) {
     log.warn(warnStr);
   }
 }

 
  public static void error(Class clazz, String errStr) {
   Log log = LogFactory.getLog(clazz);
   if (log.isErrorEnabled()) {
     log.error(errStr);
   }
  }
  
  /*
   * 可以导出错误异常栈信息
   */
   
  public static void error(Class clazz, String errStr, Exception ex){
      Log log = LogFactory.getLog(clazz);
      if(log.isErrorEnabled()){
          log.error(getStackTrace(ex).append(errStr));
      }
  }
  
 public static void error(Class clazz, Exception ex){
      Log log = LogFactory.getLog(clazz);
      if(log.isErrorEnabled()){
          log.error(getStackTrace(ex));
      }
  }
  
  
  /*
   * 异常堆栈包装，可以提供更详细的错误信息
   */
  public static StringBuffer getStackTrace(Exception ex){
      StringWriter sw = new StringWriter();
      PrintWriter  pw = new PrintWriter(sw);
      ex.printStackTrace(pw);
      return sw.getBuffer();
  }

}